local SceneBase = require("app.Base.SceneBase")
local MapLayer = require("app.layer.MapLayer"):GetInstance()
local ActorManage = require("app.manage.ActorManage"):GetInstance()
local GameScene = class("GameScene", SceneBase)

function GameScene:ctor(data)
    print("GameScene")
    GameScene.super.ctor(self,data)
    self:initUI()    

    self.Contro = { Play = false , Stepping = false }
end

function GameScene:initUI()
    self.bg = display.newSprite("UI/bg/bg.jpg"):addTo(self):move(display.cx,display.cy)
    self.MapLayer = MapLayer:addTo(self)    

    self.RightNode = display.newNode()
    self.RightNode:move(850,display.cy):addTo(self)    

    self:ControlUI()
end

function GameScene:ControlUI()
    self.Play_btn = td.CreateBtn({text={str="开始"},cb = handler(self,self.ClickControPlay)}):addTo(self.RightNode):move(-100,300)

    self.Stepping_btn = td.CreateBtn({text={str="步进"},cb = handler(self,self.ClickControStepping)}):addTo(self.RightNode):move(100,300)

    self.text_binghzong = td.CreateLabel("兵种")
    self.text_binghzong:move(0,50):addTo(self.RightNode)

    self.Solied = ccui.EditBox:create(cc.size(350, 50), "UI/s9/9.png")
    self.Solied:setFontSize(18)
    self.Solied:setMaxLength(5)
    self.Solied:setPlaceHolder("1:骷髅2:弓箭手3:胖子")
    self.Solied:move(-20,0):addTo(self.RightNode)

    self.text_mubiao = td.CreateLabel("目标")
    self.text_mubiao:move(0,-50):addTo(self.RightNode)

    self.Target_x = ccui.EditBox:create(cc.size(100, 50), "UI/s9/9.png")
    self.Target_x:setFontSize(24)
    self.Target_x:setMaxLength(5)
    self.Target_x:setPlaceHolder("X")
    self.Target_x:move(-50,-100):addTo(self.RightNode)

    self.Target_y = ccui.EditBox:create(cc.size(100, 50), "UI/s9/9.png")
    self.Target_y:setFontSize(24)
    self.Target_y:setMaxLength(5)
    self.Target_y:setPlaceHolder("Y")
    self.Target_y:move(50,-100):addTo(self.RightNode)

    self.text_zuobiao = td.CreateLabel("坐标")
    self.text_zuobiao:move(0,-150):addTo(self.RightNode)

    self.Soldiers_x = ccui.EditBox:create(cc.size(100, 50), "UI/s9/9.png")
    self.Soldiers_x:setFontSize(24)
    self.Soldiers_x:setMaxLength(5)
    self.Soldiers_x:setPlaceHolder("X")
    self.Soldiers_x:move(-50,-200):addTo(self.RightNode)

    self.Soldiers_y = ccui.EditBox:create(cc.size(100, 50), "UI/s9/9.png")
    self.Soldiers_y:setFontSize(24)
    self.Soldiers_y:setMaxLength(5)
    self.Soldiers_y:setPlaceHolder("Y")
    self.Soldiers_y:move(50,-200):addTo(self.RightNode)

    
    self.text_wanjia = td.CreateLabel("玩家")
    self.text_wanjia:move(-150,-150):addTo(self.RightNode)

    self.Player = ccui.EditBox:create(cc.size(100, 50), "UI/s9/9.png")
    self.Player:setFontSize(24)
    self.Player:setMaxLength(5)
    self.Player:setPlaceHolder("1")
    self.Player:move(-150,-200):addTo(self.RightNode)

    self.text_shuliang = td.CreateLabel("数量")
    self.text_shuliang:move(-200,-275):addTo(self.RightNode)

    self.Num = ccui.EditBox:create(cc.size(100, 50), "UI/s9/9.png")
    self.Num:setFontSize(24)
    self.Num:setMaxLength(1)
    self.Num:setPlaceHolder("1")
    self.Num:move(-100,-275):addTo(self.RightNode)

    self.Soldiers_btn = td.CreateBtn({text={str="加兵"},cb = handler(self,self.ClickAddSoldier)}):addTo(self.RightNode):move(100,-275):setTag(0)

    self.AddToPlayer1_btn = td.CreateBtn({text={str="加给玩家1"},cb = handler(self,self.ClickAddSoldier)}):addTo(self.RightNode):move(-100,-350):setTag(1)
    self.AddToPlayer2_btn = td.CreateBtn({text={str="加给玩家2"},cb = handler(self,self.ClickAddSoldier)}):addTo(self.RightNode):move(100,-350):setTag(2)

    --test
    self.Player:setText("1")
    self.Target_x:setText("501")
    self.Target_y:setText("502")
    self.Soldiers_x:setText("101")
    self.Soldiers_y:setText("102")
    self.Num:setText("1")
end

function GameScene:ClickControPlay()
    self.Contro.Play = not self.Contro.Play
    if self.Contro.Play then 
        td.BtnAddText(self.Play_btn,{str="暂停"})
    else 
        td.BtnAddText(self.Play_btn,{str="继续"})
    end
end

function GameScene:ClickControStepping()
    self.Contro.Play = false
    td.BtnAddText(self.Play_btn,{str="继续"})
    self.Contro.Stepping = true
end


function GameScene:ClickAddSoldier(btn)
    local player = btn:getTag()
    local reverse = false
    if player == 2 then reverse = true end
    if player == 0 then player = tonumber(self.Player:getText()) end

    local Solied = tonumber(self.Solied:getText())
    if Solied == nil then Solied = 1 end

    local posX = tonumber(self.Soldiers_x:getText()) 
    local posY = tonumber(self.Soldiers_y:getText())
    local targetX = tonumber(self.Target_x:getText())
    local targetY = tonumber(self.Target_y:getText()) 
    if reverse then posX,posY,targetX,targetY = targetX,targetY,posX,posY end
    print("给玩家"..player.."加兵在 X:"..posX.." Y:"..posY.." 目标是 X:"..targetX.." Y:"..targetY)

    local data = {}
    data._pos = { x = posX , y = posY}
    data._target = { }
    data._target.pos = { {x = targetX , y = targetY}  }
    data.player = player

    local Texture 
    if Solied == 1 then     Texture = "soldiers.kulou"
    elseif Solied == 2 then     Texture = "soldiers.gongjian"
    elseif Solied == 3 then     Texture = "soldiers.pangzi"
    end

    local num = tonumber(self.Num:getText()) 
    if Texture then
        for i=1,num do
            data._pos = cc.pAdd(data._pos,{x=i,y=-i})
            self:AddSoldier(Texture,data)
        end
    end
end

function GameScene:AddSoldier(name,data)
    local object = ActorManage:newActor(name,data)
    if object then 
        self.MapLayer:addChild(object)
--        object.Sprite:move(object._pos)
    end
end

function GameScene:Update(dt)
    if self.Contro.Play or self.Contro.Stepping then 
        if self.Contro.Stepping then self.Contro.Stepping = false end
        self.MapLayer:Update(dt)
        ActorManage:Update(dt)        
    end 
end


return GameScene